import util from '../util.js';
import pool from './db.js';

// 查询所有模板
export async function getAllVoices() {
    const [rows] = await pool.query('SELECT * FROM voice');
    return rows;
}

// 根据ID查询模板
export async function getVoiceById(id) {
    const [rows] = await pool.query('SELECT * FROM voice WHERE id = ?', [id]);
    return rows[0];
}

// 添加模板
export async function addVoice(voice) {
    const { name, workflow_id } = voice;
    const now = util.getDatetime();
    const [result] = await pool.query(
        'INSERT INTO voice (name, workflow_id, create_time, update_time) VALUES (?, ?, ?, ?)',
        [name, workflow_id, now, now]
    );
    return result.insertId;
}

// 更新模板
export async function updateVoice(id, voice) {
    const { name, workflow_id } = voice;
    const now = util.getDatetime();
    const [result] = await pool.query(
        'UPDATE voice SET name = ?, workflow_id = ?, update_time = ? WHERE id = ?',
        [name, workflow_id, now, id]
    );
    return result.affectedRows > 0;
}

// 删除模板
export async function deleteVoice(id) {
    const [result] = await pool.query('DELETE FROM voice WHERE id = ?', [id]);
    return result.affectedRows > 0;
}
